package com.HouseManage.view;

import com.HouseManage.beans.House;
import com.HouseManage.utils.JdbcUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class View extends JFrame {
    JTextField name,tips,year,price;
    JComboBox<String > comboBox;
    JLabel xiaoqu,fanghao,nianfen,jiage,zhuangtai;
    JButton add,del,search,change;
    JTextArea show;
    public View() {
        init();
    }

    private void init() {
        setVisible(true);
        name = new JTextField("10");
        tips = new JTextField("10");
        year = new JTextField("10");
        price = new JTextField("10");
        xiaoqu = new JLabel("小区");
        fanghao = new JLabel("房号");
        nianfen = new JLabel("年份");
        jiage = new JLabel("价格");
        add = new JButton("添加");
        del = new JButton("删除");
        search = new JButton("查找");
        change = new JButton("更改");
        zhuangtai = new JLabel("状态");
        comboBox = new JComboBox<String>();
        comboBox.addItem("出租");
        comboBox.addItem("空闲");
        show = new JTextArea(30,30);
        show.setEnabled(false);
        show.append("增加房间信息需要输入所有内容\n"+"删除需要确认房间名以及房间号正确,搜索同理\n"+"更改需要确认房间名及房间号，其余为希望更改的内容\n");
        Box big = Box.createVerticalBox();
        add(big);
        Box box = Box.createHorizontalBox();
        Box box1 = Box.createHorizontalBox();
        box.add(xiaoqu);
        box.add(name);
        box.add(fanghao);
        box.add(tips);
        box.add(nianfen);
        box.add(nianfen);
        box.add(year);
        box.add(jiage);
        box.add(price);
        box.add(comboBox);
        big.add(box);
        box1.add(add);
        box1.add(del);
        box1.add(search);
        box1.add(change);
        big.add(box1);
        big.add(show);
        pack();
        add.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                boolean bo=false;
                String str = name.getText();
                String str1 = year.getText();
                String str2 = tips.getText();
                String str3 = price.getText();
                String str4 = (String) comboBox.getSelectedItem();
                System.out.println(str4.equals("出租"));
                if(str4.equals("出租")){
                    bo = true;
                }
                System.out.println(bo);
                show.append(new JdbcUtils().addMessage(new House(str,str1,str3,str2,bo))+"\n");
            }
        });

        del.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String str = name.getText();
                String str1 = year.getText();
                String str2 = tips.getText();
                String str3 = price.getText();
                show.append(new JdbcUtils().delMessage(new House(str,str1,str3,str2))+"\n");
            }
        });

        search.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                boolean bo=true;
                String str = name.getText();
                String str1 = year.getText();
                String str2 = tips.getText();
                String str3 = price.getText();
                String str4 = (String) comboBox.getSelectedItem();
                if(str4.equals("出租")){
                    bo = true;
                }else
                    bo = false;
                show.append(new JdbcUtils().searchMessage(new House(str,str1,str3,str2,bo))+"\n");
            }
        });

        change.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                boolean bo;
                String str = name.getText();
                String str1 = year.getText();
                String str2 = tips.getText();
                String str3 = price.getText();
                String str4 = (String) comboBox.getSelectedItem();
                if(str4.equals("出租")){
                    bo = true;
                }else
                    bo = false;
                show.append(new JdbcUtils().setMessage(new House(str,str1,str3,str2,bo))+"\n");
            }
        });
    }


}
